package com.graduationdesign.zengyan.graduationdesignapp.ui.base;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;

import com.graduationdesign.zengyan.graduationdesignapp.R;

/**
 * Created by ZengYan on 2018/5/5.
 *
 * @desc :
 */
public class BaseUiView extends RelativeLayout implements BaseLoadingView{

    private RelativeLayout mLoadingView;
    private View rootView;

    public BaseUiView(Context context) {
        super(context);
        initView();
    }

    public BaseUiView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BaseUiView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    private void initView() {
        rootView = LayoutInflater.from(getContext()).inflate(R.layout.base_view, this);
        mLoadingView = (RelativeLayout) rootView.findViewById(R.id.loading_view);
    }


    @Override
    public boolean isLoading() {
        return mLoadingView.getVisibility() == VISIBLE;
    }

    @Override
    public void startLoading() {
        rootView.setVisibility(VISIBLE);
        mLoadingView.setVisibility(VISIBLE);
    }

    @Override
    public void stopLoading() {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);//初始化操作，参数传入0和1，即由透明度0变化到透明度为1
        mLoadingView.startAnimation(alphaAnimation);//开始动画
        alphaAnimation.setFillAfter(true);//动画结束后保持状态
        alphaAnimation.setDuration(500);//动画持续时间，单位为毫秒
        mLoadingView.setVisibility(GONE);
        rootView.setVisibility(VISIBLE);
    }
}
